<template>
  <div class="smart-hotel-layout">
    <el-container>
      <!-- 智能侧边栏 -->
      <el-aside
        :width="isCollapse ? '64px' : '220px'"
        class="smart-sidebar"
        :class="{ 'is-collapse': isCollapse }"
      >
        <div class="sidebar-header" @click="toggleCollapse">
          <transition name="logo-fade">
            <el-icon v-if="isCollapse" class="smart-logo"
              ><MagicStick
            /></el-icon>
            <div v-else class="full-logo">
              <el-icon class="smart-logo"><MagicStick /></el-icon>
              <span class="system-name">智能酒店管理系统</span>
            </div>
          </transition>
        </div>

        <!-- 智能菜单系统 -->
        <el-scrollbar class="menu-scroll">
          <el-menu
            :router="true"
            :default-active="$route.path"
            :collapse="isCollapse"
            background-color="#c4cbcf"
            active-text-color="#ff9900"
            text-color="#1781b5"
          >
            <el-menu-item index="/system/Main/MainView">
              <el-icon><MagicStick /></el-icon>
              <span>概况</span>
            </el-menu-item>
            <el-menu-item index="/RoomStatus/RoomStatus">
              <el-icon><DataLine /></el-icon>
              <span>房态</span>
            </el-menu-item>
            <el-menu-item index="/Order/AccommodationOrders/BookingOrder">
              <el-icon><Monitor /></el-icon>
              <span>订单</span>
            </el-menu-item>
            <el-menu-item index="/Business">
              <el-icon><Connection /></el-icon>
              <span>业务</span>
            </el-menu-item>
            <el-menu-item index="/Custorm">
              <el-icon><Opportunity /></el-icon>
              <span>客户</span>
            </el-menu-item>
            <el-menu-item index="/404">
              <el-icon><Promotion /></el-icon>
              <span>营销</span>
            </el-menu-item>
            <el-menu-item index="/404">
              <el-icon><BrushFilled /></el-icon>
              <span>装修</span>
            </el-menu-item>
            <el-menu-item index="/404">
              <el-icon><DataAnalysis /></el-icon>
              <span>数据</span>
            </el-menu-item>
            <el-menu-item index="/404">
              <el-icon><ChatDotRound /></el-icon>
              <span>微信</span>
            </el-menu-item>
            <el-menu-item index="/404">
              <el-icon><Setting /></el-icon>
              <span>设置</span>
            </el-menu-item>
            <el-menu-item index="/404">
              <el-icon><Money /></el-icon>
              <span>慧付</span>
            </el-menu-item>

            <!-- 更多智能菜单项 -->
          </el-menu>
        </el-scrollbar>
      </el-aside>

      <el-container>
        <!-- 智慧头部 -->
        <el-header class="smart-header">
          <div class="header-content">
            <!-- 智能面包屑 -->
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item
                v-for="(item, index) in breadcrumbs"
                :key="index"
                :to="item.path"
              >
                {{ item.meta.title }}
              </el-breadcrumb-item>
            </el-breadcrumb>

            <!-- 智能控制区 -->
            <div class="smart-controls">
              
              <el-dropdown>
                <div class="user-panel">
                  <el-avatar
                    :size="36"
                    src="https://avatars.githubusercontent.com/u/44761321"
                  />
                  <span class="user-name">管理员</span>
                </div>
                <!-- 新增下拉菜单 -->
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="RBAC">个人中心</el-dropdown-item>
                    <el-dropdown-item divided @click="back()"
                      >退出登录</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>

        <!-- 主内容区 -->
        <el-main class="smart-main">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script setup>
import { ref, computed, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  MagicStick,
  DataLine,
  Monitor,
  Connection,
  Opportunity,
  Promotion,
  BrushFilled,
  DataAnalysis,
  ChatDotRound,
  Setting,
  Money,
} from "@element-plus/icons-vue";

const router = useRouter();
const route = useRoute();
const isCollapse = ref(false);

// 修复：增加空值保护
const breadcrumbs = computed(() => {
  return route.matched
    .filter((item) => item.meta?.title)
    .map((item) => ({
      path: item.path,
      title: item.meta.title || "未命名",
    }));
});

const RBAC = () => {
  router.push("/RBAC/RBACManage/User");
};
const back = () => {
  router.push("/");
};
// 修复：添加 mounted 错误处理
onMounted(() => {
  try {
    // 初始化逻辑（示例）
    if (!route.matched.length) {
      throw new Error("路由匹配失败");
    }
  } catch (error) {
    console.error("Mounted 钩子错误:", error);
  }
});
</script>
<style lang="scss" scoped>
@use "sass:color"; // 添加 Sass 模块引用

$sidebar-bg: #c4cbcf;
$header-bg: linear-gradient(145deg, #85aeff 0%, #d9dee4 100%);
$primary-color: #fffb00;

/// 图标样式
.smart-hotel-layout {
  height: 100vh;
  overflow: hidden;

  .smart-sidebar {
    background: $sidebar-bg;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);

    .sidebar-header {
      height: 64px;
      display: flex;
      align-items: center;
      padding: 0 20px;
      cursor: pointer;
      border-bottom: 1px solid rgba(251, 40, 40, 0.1);

      .full-logo {
        display: flex;
        align-items: center;
        gap: 12px;
      }

      .smart-logo {
        font-size: 28px;
        color: $primary-color;
        flex-shrink: 0;
      }

      .system-name {
        font-size: 18px;
        font-weight: 500;
        color: #022262;
        white-space: nowrap;
        font-family: "Arial", sans-serif;
        line-height: 1.6;
        letter-spacing: 0.5px;
        text-transform: capitalize;
        text-shadow: 1px 1px 4px #00000018;
        font-family: "SimSun", "Times New Roman", Times, serif;
      }
    }

    &.is-collapse {
      .el-menu-item {
        padding: 0 18px !important;

        span {
          opacity: 0;
          transition: opacity 0.2s;
        }
      }
    }
  }

  /// 菜单样式
  .menu-scroll {
    height: calc(100vh - 64px);

    .el-menu {
      border-right: none;

      .el-menu-item {
        margin: 8px;
        border-radius: 8px;
        transition: all 0.3s;

        &:hover {
          background: rgba($primary-color, 0.1) !important;
        }

        &.is-active {
          background: linear-gradient(
            90deg,
            color.scale($primary-color, $alpha: -80%),
            transparent
          );
          position: relative;

          &::after {
            content: "";
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            height: 60%;
            width: 3px;
            background: $primary-color;
            border-radius: 2px;
          }
        }

        .el-icon {
          margin-right: 12px;
          font-size: 20px;
        }
      }
    }
  }

  /// 头部样式
  .smart-header {
    height: 64px;
    background: $header-bg;
    box-shadow: 0 2px 8px rgba(152, 16, 16, 0.316);

    .header-content {
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 24px;

      :deep(.el-breadcrumb) {
        font-size: 15px;

        .el-breadcrumb__inner {
          color: #a4b0be;

          &.is-link {
            color: $primary-color;
            transition: color 0.3s;

            &:hover {
              color: adjust-color($primary-color, $lightness: 10%); // 修正颜色函数
            }
          }
        }
      }

      .smart-controls {
        display: flex;
        align-items: center;
        gap: 24px;

        .el-button {
          color: #a4b0be;
          transition: all 0.3s;

          &:hover {
            color: adjust-color($primary-color, $lightness: 10%); // 修正颜色函数
            transform: scale(1.1);
          }
        }

        .user-panel {
          display: flex;
          align-items: center;
          gap: 12px;
          cursor: pointer;

          .user-name {
            color: #fff;
            font-size: 14px;
          }
        }
      }
    }
  }

  .smart-main {
    background: #f5f7fa;
    padding: 20px;
    height: calc(100vh - 64px);
    overflow: auto;
  }
}

// 过渡动画
.logo-fade-enter-active,
.logo-fade-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.logo-fade-enter-from,
.logo-fade-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}

// 动态隐藏滚动条
.menu-scroll,
.smart-main {
  scrollbar-width: none;
  -ms-overflow-style: none;

  &::-webkit-scrollbar {
    width: 0;
    height: 0;
    background-color: transparent;
    transition: all 0.3s;
  }

  &:hover {
    &::-webkit-scrollbar {
      width: 6px;
      height: 6px;
      background-color: rgba(245, 247, 250, 0.8);
    }

    &::-webkit-scrollbar-thumb {
      background-color: rgba(144, 147, 153, 0.3);
    }
  }
}
</style>